package zhieasy.cn.service.common.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import zhieasy.cn.common.enums.UserVerifyEnum;
import zhieasy.cn.common.form.StudentAuthForm;
import zhieasy.cn.common.utils.CmdScriptUtils;
import zhieasy.cn.model.common.StudentUser;
import zhieasy.cn.persistence.mapper.common.StudentUserMapper;
import zhieasy.cn.service.common.StudentUserService;
import zhieasy.cn.service.common.script.AuthCodeSession;
import zhieasy.cn.service.common.script.StudentAuthInfoData;
import zhieasy.cn.service.common.script.StudentInfoRequest;

import javax.annotation.Resource;
import java.util.Objects;


/**
 * 学生信息认证
 * @author zouchanglin
 */
@Slf4j
@Service
public class StudentUserServiceImpl extends ServiceImpl<StudentUserMapper,StudentUser> implements StudentUserService {
    @Resource
    private CommonInitServiceImpl commonInitService;

    @Resource
    private StudentUserMapper studentUserMapper;

    // openId -> SessionId Cache
    @Override
    public String getAuthCodeImage(String openId) {
        return commonInitService.getAuthCodeImage(openId);
    }

    @Override
    public StudentUser authStudentInfo(StudentAuthForm studentAuthForm, String wxOpenId) {
        AuthCodeSession authCodeData = CommonInitServiceImpl.weChatSessionCache.get(wxOpenId);
        AuthCodeSession.Data codeDataData = authCodeData.getData();
        StudentInfoRequest request = new StudentInfoRequest(
                studentAuthForm.getAuthCode(),
                studentAuthForm.getStudentId(),
                studentAuthForm.getPassword(),
                codeDataData.getLocation_url(),
                codeDataData.getHave_location_url()
        );

        String[] cmd = new String[]{"python", CommonInitServiceImpl.userInfoScript.getAbsolutePath(), JSON.toJSONString(request)};
        String result = CmdScriptUtils.execCmd(cmd);
        log.info("result = {}", result);

        String dataStr = JSON.parseObject(result).getString("data");
        log.info("dataStr = {}", dataStr);

        String error = JSON.parseObject(result).getString("error");
        log.info("errorCode = {}", error);

        StudentAuthInfoData data = JSON.parseObject(dataStr, StudentAuthInfoData.class);
        log.info("StudentAuthInfoData = {}", data);

        if("0".equals(error)){
            StudentUser studentUser = studentUserMapper.selectById(wxOpenId);
            boolean flag = false;
            if(studentUser == null){
                flag = true;
                studentUser = new StudentUser();
            }
            studentUser.setCreateTime(System.currentTimeMillis());
            studentUser.setOpenId(wxOpenId);
            studentUser.setStudentName(data.getUser_name());
            studentUser.setCollegeId(data.getUser_college());
            studentUser.setGradeId(data.getUser_class());
            studentUser.setUpdateTime(System.currentTimeMillis());
            studentUser.setStudentId(studentAuthForm.getStudentId());
            studentUser.setStudentStatus(UserVerifyEnum.VERIFY_PASS.getCode());
            if(flag){
                int insert = studentUserMapper.insert(studentUser);
                log.info("insert result {}", insert);
            }else{
                int update = studentUserMapper.updateById(studentUser);
                log.info("update result {}", update);
            }
            CommonInitServiceImpl.weChatSessionCache.remove(wxOpenId);
            return studentUser;
        }
        return null;
    }

    @Override
    public Boolean isHaveStudentUser(String openId) {
        StudentUser studentUser = studentUserMapper.selectById(openId);
        //说明注册过
        return Objects.isNull(studentUser);//说明没有注册
    }
}
